package com.example.service;

import com.example.entity.ImageFiles;
import com.example.entity.MriImages;
import com.example.exception.CustomException;
import com.example.utils.OSSUtil;
import com.example.utils.TempFileUtil;
import com.sun.istack.NotNull;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: 张比特
 * @Data: 2024/12/13 下午8:06
 * @Version: v1.0.0
 * @Description: TODO 文件接口的服务类
 **/
@Service
public class UploadService {

    private static final Logger log = LoggerFactory.getLogger(UploadService.class);
    @Autowired
    FileStorageService fileStorageService;
    @Autowired
    ImageFilesService imageFilesService;

    /**
     * 头像上传
     *
     * @param file
     * @return
     */
    public String uploadAvatar(MultipartFile file) {
        try {
            FileInfo fileInfo = fileStorageService.of(file)
                    .setPath(new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/avatar/")
                    .setSaveFilename(OSSUtil.getUUID(file))
                    .upload();
            return OSSUtil.getOSSURL() + fileInfo.getUrl();
        } catch (Exception e) {
            throw new CustomException("文件上传错误");
        }
    }

    /**
     * .nii文件上传
     *
     * @param file
     * @return
     */
    public FileInfo uploadNiiFile(MultipartFile file) {
        try {
            FileInfo fileInfo = fileStorageService.of(file)
                    .setPath(new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/nii/")
                    .setSaveFilename(OSSUtil.getUUID(file))
                    .upload();
            return fileInfo;
        } catch (Exception e) {
            throw new CustomException("文件上传错误");
        }
    }

    /**
     * 将png文件单个上传到OSS平台
     *
     * @param pngDest
     * @return
     */
    public String uploadPngFileSingle(Path pngDest, String seed, String pathName) {
        try {
            List<String> axial = new ArrayList<>();
            List<String> coronal = new ArrayList<>();
            List<String> sagittal = new ArrayList<>();

            File rootDirectory = new File(pngDest.toString());
            if (rootDirectory.exists() && rootDirectory.isDirectory()) {
                File[] directorys = rootDirectory.listFiles();
                for (File directory : directorys) {
                    log.info(directory.getName());
                    File[] files = directory.listFiles();
                    if (files != null) {
                        for (File file : files) {
                            if (file.isFile()) {
                                String fileName = file.getName();
                                // 删除前缀
                                if (file.getParentFile().toString().contains("axial")) {
                                    FileInfo fileInfo = fileStorageService.of(file)
                                            .setPath(new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + seed + "/Axial/")
                                            .setSaveFilename(fileName)
                                            .upload();
                                    log.info(fileInfo.getUrl());
                                    axial.add(OSSUtil.getOSSURL() + fileInfo.getUrl());
                                } else if (file.getParentFile().toString().contains("coronal")) {
                                    FileInfo fileInfo = fileStorageService.of(file)
                                            .setPath(new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + seed + "/Coronal/")
                                            .setSaveFilename(fileName)
                                            .upload();
                                    log.info(fileInfo.getUrl());
                                    coronal.add(OSSUtil.getOSSURL() + fileInfo.getUrl());
                                } else if (file.getParentFile().toString().contains("sagittal")) {
                                    FileInfo fileInfo = fileStorageService.of(file)
                                            .setPath(new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + seed + "/Sagittal/")
                                            .setSaveFilename(fileName)
                                            .upload();
                                    log.info(fileInfo.getUrl());
                                    sagittal.add(OSSUtil.getOSSURL() + fileInfo.getUrl());
                                }
                            }
                        }
                    }
                }
            }

            // 将文件路径上传至数据库
            ImageFiles imageFilesA = new ImageFiles();
            imageFilesA.setNiiFileName(pathName);
            imageFilesA.setFileName(axial.toString());
            imageFilesA.setViewType("Axial");
            imageFilesA.setSeed(seed);
            imageFilesService.add(imageFilesA);

            ImageFiles imageFileC = new ImageFiles();
            imageFileC.setNiiFileName(pathName);
            imageFileC.setFileName(coronal.toString());
            imageFileC.setViewType("Coronal");
            imageFileC.setSeed(seed);
            imageFilesService.add(imageFileC);

            ImageFiles imageFileS = new ImageFiles();
            imageFileS.setNiiFileName(pathName);
            imageFileS.setFileName(sagittal.toString());
            imageFileS.setViewType("Sagittal");
            imageFileS.setSeed(seed);
            imageFilesService.add(imageFileS);

            TempFileUtil.deleteTempPath(pngDest);
        } catch (Exception e) {
            throw new CustomException("文件上传错误");
        }
        return null;
    }

    /**
     * 以文件夹的形式将文件上传到OSS上
     * OSS无法直接上传整个文件夹
     *
     * @param path
     * @return
     */
    public String uploadPngFileDirectory(@NotNull Path path) {
        File file = new File(path.toString());
        String fileName = file.getName().substring("png-".length());
        FileInfo fileInfo = fileStorageService.of()
                .setPath(new SimpleDateFormat("yyyyMMdd").format(new Date()))
                .setSaveFilename(fileName)
                .upload();
        return null;
    }
}
